/**
 * Created by on 2016/7/10.
 */

var mysql = require('knex');

class masterModel {

	constructor(){

		this.client={};

	}

	createClient(options){

		this.client=mysql({"client":"mysql","connection":options});
		
	}
   
    formatAlipay(params){
        let newdata = {}
        newdata.creater=params.subject.substring(params.subject.indexOf('_')+1,params.subject.length);
        newdata.subject=params.subject;
        newdata.body=params.body;
        newdata.buyer_id=params.buyer_id;
        newdata.fund_bill_list=params.fund_bill_list;
        newdata.receipt_amount=params.receipt_amount;
        newdata.out_trade_no=params.out_trade_no;
        newdata.buyer_logon_id=params.buyer_logon_id;
        newdata.gmt_create=params.gmt_create;
        newdata.notify_time=params.notify_time;
        newdata.trade_status=params.trade_status;
        newdata.status=params.status;
        newdata.trade_no=params.trade_no;
        newdata.buyer_pay_amount=params.buyer_pay_amount;
        newdata.total_amount=params.total_amount;
        newdata.notify_type=params.notify_type;
        newdata.status=1;
        return newdata;
    }

    additionAlipay(params){
        var dataObj=this.formatAlipay(params)
        console.log(dataObj);
        return new Promise((resolve, reject)=>{
            this.client('redis_information').insert(dataObj)
                .then(result=>resolve(result))
                .catch(err=>reject(err))
        })
    }

    SyncData(){
        return new Promise((resolve, reject)=>{
            this.client('alipay_infomation').select().limit(1)
                .then(result=>resolve(result))
                .catch(err=>reject(err))
        })
    }

    getAdminByPassword(username,password){
        return new Promise((resolve,reject)=>{
            this.client("admin").select("*").where({"username":username,"password":password})
                .then(result=>resolve(result))
                .catch(err=>reject(err))
          })
    }




}

module.exports = new masterModel();
